﻿using BaoVeBienCuong.Playgame.army;
using System;
using System.IO;
using System.IO.IsolatedStorage;

namespace BaoVeBienCuong.SelectMap
{
    public class SaveKyNang
    {
        LinhCanChien lcc;
        static Boolean[] thongso = new Boolean[16];
        public SaveKyNang()
        {
            lcc = new LinhCanChien();
            getFile();
        }
        public void getFile()
        {
            int index = 0;
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!storage.FileExists("sunnet_l12sq_SaveKyNang.txt"))
            {
                using (var writer = new StreamWriter(
               new IsolatedStorageFileStream("sunnet_l12sq_SaveKyNang.txt", FileMode.Create, FileAccess.Write, storage)))
                {
                    for (int i = 0; i < 16; i++)
                        writer.WriteLine("false");
                }
            }
            IsolatedStorageFileStream f = storage.OpenFile("sunnet_l12sq_SaveKyNang.txt", FileMode.Open,
                FileAccess.Read, FileShare.Read);
            using (StreamReader r = new StreamReader(f))
            {
                do
                {
                    // temp = r.ReadLine();
                    //buttonKyNang[index].isNangCap = Boolean.Parse(temp);
                    thongso[index] = Convert.ToBoolean(r.ReadLine());
                    index++;
                } while (!r.EndOfStream);
                r.Close();
                r.Dispose();
            }
            f.Close();
            f.Dispose();

            storage.Dispose();
        }
        public static void write_KyNang(KyNang[] buttonKyNang)
        {
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (storage.FileExists("sunnet_l12sq_SaveKyNang.txt"))
            {
                storage.DeleteFile("sunnet_l12sq_SaveKyNang.txt");
            }

            using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("sunnet_l12sq_SaveKyNang.txt", FileMode.Create, FileAccess.Write, storage)))
            {
                for (int i = 0; i < 16; i++)
                    writer.WriteLine(buttonKyNang[i].isNangCap.ToString());
            }
        }
        public KyNang[] readSaveKyNang()
        {
            KyNang[] buttonKyNang = new KyNang[16];
            for (int i = 0; i < 16; i++)
            {
                buttonKyNang[i] = new KyNang(i);
            }
            int index = 0;
            //  string temp = "";
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!storage.FileExists("sunnet_l12sq_SaveKyNang.txt"))
            {
                using (var writer = new StreamWriter(
               new IsolatedStorageFileStream("sunnet_l12sq_SaveKyNang.txt", FileMode.Create, FileAccess.Write, storage)))
                {
                    for (int i = 0; i < 16; i++)
                        writer.WriteLine("false");
                }
            }
            IsolatedStorageFileStream f = storage.OpenFile("sunnet_l12sq_SaveKyNang.txt", FileMode.Open,
                FileAccess.Read, FileShare.Read);
            using (StreamReader r = new StreamReader(f))
            {
                do
                {
                    // temp = r.ReadLine();
                    //buttonKyNang[index].isNangCap = Boolean.Parse(temp);
                    buttonKyNang[index].isNangCap = Convert.ToBoolean(r.ReadLine());
                    index++;
                } while (!r.EndOfStream);
                r.Close();
                r.Dispose();
            }
            f.Close();
            f.Dispose();

            storage.Dispose();
            return buttonKyNang;
        }
        public Info GetValue(string loai, int level, int sao)
        {
            int tangsatthuong = 0;
            int tangculi = 0;
            int tangtocdo = 0;
            Info info = lcc.getInfo(loai, level, sao);
            if (loai.Equals("sword") || loai.Equals("knight"))
            {
                #region
                if (thongso[0] == true)
                {
                    tangsatthuong += 25;
                    if (loai.Equals("sword") && level == 2) tangculi += 64;
                }
                if (thongso[1] == true)
                {
                    tangculi += 64;
                    if (loai.Equals("sword") && level == 1) tangtocdo += 50;
                }
                if (thongso[2] == true)
                {
                    if (loai.Equals("knight") && level == 2) tangsatthuong += 50;
                }
                if (thongso[3] == true)
                {
                    if (level == 1 || level == 2) tangsatthuong += 100;
                    if (loai.Equals("knight") && level == 1) tangsatthuong += 50;
                }
                #endregion
            }
            else if (loai.Equals("archer") || loai.Equals("shaman"))
            {
                #region
                if (thongso[4] == true)
                {
                    tangculi += 64;
                    if (loai.Equals("archer") && level == 1) tangsatthuong += 50;
                }
                if (thongso[5] == true)
                {
                    tangsatthuong += 50;
                    if (loai.Equals("archer") && level == 2) tangsatthuong += 50;
                }
                if (thongso[6] == true)
                {
                    if (loai.Equals("shaman") && level == 2) tangculi += 64;
                }
                if (thongso[7] == true)
                {
                    if (level == 1 || level == 2) tangsatthuong += 100;
                }
                #endregion
            }
            info.setSatthuong(info.getSatthuong() * (1 + tangsatthuong / 100));
            info.setTocdo(info.getTocdo() * (1 + tangtocdo / 100));
            info.setTamxa(info.getTamXa() + tangculi);
            return info;
        }
        public int getPrice(int loai, int price)
        {
            if (thongso[2] == true)
            {
                if (loai == 5 || loai == 8)
                {
                    price = (int)(price * 3 / 4);
                }
            }
            if (thongso[6] == true)
            {
                if (loai == 6 || loai == 7)
                {
                    price = (int)(price * 3 / 4);
                }
            }
            return price;
        } // dành cho việc đặt tướng
        public static void CheckNangCapPrice(int loai, ref int price) // dành cho việc nâng cấp
        {
            if (thongso[2] == true)
            {
                if (loai == 0 || loai == 3)
                {
                    price = (int)(price * 3 / 4);
                }
            }
            if (thongso[6] == true)
            {
                if (loai == 1 || loai == 2)
                {
                    price = (int)(price * 3 / 4);
                }
            }
        }
        public int getFire(int loai, ref float Dame)
        {
            int satthuong = 0;
            int soluong = 0;
            if (loai == 0)
            {
                if (thongso[8] == true) satthuong += 1;
                if (thongso[9] == true) soluong += 5;
                if (thongso[10] == true) satthuong += 1;
                if (thongso[11] == true) satthuong += 3;
                Dame = satthuong;
            }
            else
            {
                if (thongso[8] == true) soluong += 2;
                if (thongso[9] == true) soluong += 5;
                if (thongso[10] == true) satthuong += 2;
                if (thongso[11] == true) satthuong += 3;
                Dame = satthuong + 4;
            }
            return soluong + 2;
        }
        public int getFire(int loai)
        {
            int soluong = 0;
            if (loai == 0)
            {
                if (thongso[9] == true) soluong += 5;
            }
            else
            {
                if (thongso[8] == true) soluong += 2;
                if (thongso[9] == true) soluong += 5;
            }
            return soluong + 2;
        }
    }
}
